<?php

require_once 'pojos/Cookie.php';
require_once 'pojos/CookieType.php';
require_once 'model/CookieDao.php';
require_once 'model/CookieTypeDao.php';

function uploadImage() {
    $fileName = '';
    if ($_FILES['image']['error'] > 0) {        
        echo 'Error: ' . $_FILES['image']['error'] . '<br />';
    } else {        
        $fileName = 'images/cookies/' . $_FILES['image']['name'];
        
        move_uploaded_file($_FILES['image']['tmp_name'], $fileName);
        //echo 'Upload: ' . $_FILES['image']['name'] . '<br />';
        //echo "Type: " . $_FILES["file"]["type"] . "<br />";
        //echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
        //echo "Stored in: " . $_FILES["file"]["tmp_name"];
    }
    return $fileName;
}

if (isset($_POST['AddCookie'])) {
    $imageUploadedUrl = uploadImage();
    $cookie = new Cookie();
    $cookie->setName($_POST['cookieName']);
    $cookie->setCookieType($_POST['cookieType']);
    $cookie->setPrice($_POST['price']);
    $cookie->setDescription($_POST['description']);
    $cookie->setImage($imageUploadedUrl);
    
    $cookieDao = new CookieDao();
    $cookieDao->addCookie($cookie);
}


$page_title = "Thêm Bánh";
$page_content = 'page_content/thembanh_content.php';
$menubottom = 'page_content/bottomMenu.php';
$menu = 'page_content/menu.php';
include ('MasterPage.php');
?>
